<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>给所有"假如"加上[]</title>
</head>

<body>
    <script>
        var str = "假如给我三天光明,我会干什么? 假如给我帅气面孔,我会干什么? 假如给我一个大西瓜,我会干什么? 假如给我一瓶冰阔落,我会干什么?";
        var arr = str.split('');
        console.log(arr);
        for (var i = 0, len = arr.length; i < len; i++) {
            if (arr[i] === "假" && arr[i+1] === "如") {
                arr[i] = "[假";
                arr[i + 1] = "如]";
                i++//跳过"如"
            }
            
        }
        console.log(arr.join(''));
    </script>
</body>

</html>